package topo;

import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JMenu;
import javax.swing.JPopupMenu;

import topo.action.AutoDiscoverByIPRoamingAction;
import topo.action.AutoDiscoverByIpAction;
import topo.action.AutoDiscoverByNetAction;
import topo.action.AutoDiscoverByRouterAction;
import topo.action.ClearAllAction;
import topo.action.LinkDeleteAction;
import topo.action.LinkViewAction;
import topo.action.NodeAddAction;
import topo.action.NodeDeleteAction;
import topo.action.NodeViewAction;
import topo.action.Proxy;
import topo.action.ResourcesSearchAction;
import topo.action.SetLinkEndAction;
import topo.action.SetLinkStartAction;
import topo.data.NodeDevice;
import topo.data.NodeLink;

public class TopoMouseAction implements TopoMouseHandle {
    public JPopupMenu createRightClickMultiObjectsMenu(TopoGraphView topoView, List<TopoObject> topoObjList) {
        return null;
    }

    public void actionClick(TopoGraphView topoView, TopoObject object) {
    }

    public String mouseToolTip(TopoGraphView topoView, TopoObject object) {
        return "";
    }

    public JPopupMenu createRightClickMenu(TopoGraphView topoView, TopoObject object) {
        JPopupMenu menu = new JPopupMenu();

        Object userID = object.getUserID();
        if ((userID instanceof NodeDevice)) {
            setNodeRightMenu(menu, (NodeDevice) userID, topoView);
        } else if ((userID instanceof NodeLink)) {
            setNodeLinkRightMenu(menu, (NodeLink) userID, topoView);
        }

        return menu;
    }

    public JPopupMenu createRightClickBlankMenu(final TopoGraphView topoView) {
        JPopupMenu menu = new JPopupMenu();

        JMenu addDevice = new JMenu("添加设备");

        NodeAddAction nodeAddAction = new NodeAddAction();
        nodeAddAction.putValue("ActionDataTopoview", topoView);
        Proxy proxy = new Proxy();
        proxy.putValue("ActionDataTopoview", topoView);
        addDevice.add(nodeAddAction);
        addDevice.add(proxy);

        menu.add(addDevice);
        menu.addSeparator();

        ClearAllAction actionclearall = new ClearAllAction();
        actionclearall.putValue("ActionDataTopoview", topoView);
        menu.add(actionclearall);

        menu.addSeparator();

        AutoDiscoverByIpAction discoverByIPAction = new AutoDiscoverByIpAction();
        discoverByIPAction.putValue("ActionDataTopoview", topoView);
        menu.add(discoverByIPAction);

        AutoDiscoverByNetAction discoverByNetAction = new AutoDiscoverByNetAction();
        discoverByNetAction.putValue("ActionDataKeyName", topoView);
        menu.add(discoverByNetAction);

        AutoDiscoverByRouterAction discoverByRouteAction = new AutoDiscoverByRouterAction();
        discoverByRouteAction.putValue("ActionDataKeyName", topoView);
        menu.add(discoverByRouteAction);

        AutoDiscoverByIPRoamingAction discoverByIPRoamingAction = new AutoDiscoverByIPRoamingAction();
        discoverByIPRoamingAction.putValue("ActionDataKeyName", topoView);
        menu.add(discoverByIPRoamingAction);

        JMenu menunetwork = new JMenu("网络重新布局");

        menunetwork.add(new AbstractAction("Hierarchical布局") {
            public void actionPerformed(ActionEvent e) {
                TopoLayoutManager.hierarchicalLayout(topoView);
            }
        });
        menunetwork.add(new AbstractAction("快速Organic布局") {
            public void actionPerformed(ActionEvent e) {
                TopoLayoutManager.fastOrganicLayout(topoView);
            }
        });
        menunetwork.add(new AbstractAction("Organic布局") {
            public void actionPerformed(ActionEvent e) {
                TopoLayoutManager.organicLayout(topoView);
            }
        });
        menunetwork.add(new AbstractAction("Self-Organizing布局") {
            public void actionPerformed(ActionEvent e) {
                TopoLayoutManager.selfOrganicLayout(topoView);
            }
        });
        menunetwork.add(new AbstractAction("紧凑树型布局") {
            public void actionPerformed(ActionEvent e) {
                TopoLayoutManager.compactTreeLayout(topoView);
            }
        });
        menunetwork.add(new AbstractAction("放射树形布局") {
            public void actionPerformed(ActionEvent e) {
                TopoLayoutManager.radialTreeLayout(topoView);
            }
        });
        menunetwork.add(new AbstractAction("基本树形布局") {
            public void actionPerformed(ActionEvent e) {
                TopoLayoutManager.treeLayout(topoView);
            }
        });
        menunetwork.add(new AbstractAction("Organizational布局") {
            public void actionPerformed(ActionEvent e) {
                TopoLayoutManager.organizationalLayout(topoView);
            }
        });
        menunetwork.add(new AbstractAction("简单随机布局") {
            public void actionPerformed(ActionEvent e) {
                TopoLayoutManager.simpleRandomLayout(topoView);
            }
        });
        menunetwork.add(new AbstractAction("简单标题布局") {
            public void actionPerformed(ActionEvent e) {
                TopoLayoutManager.simpleTiltLayout(topoView);
            }
        });
        menunetwork.add(new AbstractAction("圆形布局") {
            public void actionPerformed(ActionEvent e) {
                TopoLayoutManager.simpleCircleLayout(topoView);
            }
        });
        menu.add(menunetwork);

        return menu;
    }

    public void doubleClickAction(TopoGraphView topoView, TopoObject obj) {
        if (obj != null) {
            Object userID = obj.getUserID();
            if ((userID instanceof NodeDevice)) {
                NodeViewAction nodeViewAction = new NodeViewAction();
                nodeViewAction.putValue("ActionDataKeyName", (NodeDevice) userID);
                nodeViewAction.actionPerformed(null);
            } else if ((userID instanceof NodeLink)) {
                LinkViewAction linkViewAction = new LinkViewAction();
                linkViewAction.putValue("ActionDataKeyName", (NodeLink) userID);
                linkViewAction.actionPerformed(null);
            }
        }
    }

    private void setNodeRightMenu(JPopupMenu menu, NodeDevice nodeDevice, TopoGraphView topoView) {
        NodeViewAction nodeViewAction = new NodeViewAction();
        nodeViewAction.putValue("ActionDataKeyName", nodeDevice);

        menu.add(nodeViewAction);
        menu.addSeparator();

        NodeDeleteAction nodeDeleteAction = new NodeDeleteAction();
        nodeDeleteAction.putValue("ActionDataKeyName", nodeDevice);

        menu.add(nodeDeleteAction);
        menu.addSeparator();

        ResourcesSearchAction resViewAction = new ResourcesSearchAction();
        resViewAction.putValue("ActionDataKeyName", nodeDevice);

        menu.add(resViewAction);
        menu.addSeparator();

        SetLinkStartAction actionSetLinkStart = new SetLinkStartAction();
        actionSetLinkStart.putValue("ActionDataTopoview", topoView);

        menu.add(actionSetLinkStart);

        SetLinkEndAction actionSetLinkEnd = new SetLinkEndAction();
        actionSetLinkEnd.putValue("ActionDataTopoview", topoView);

        menu.add(actionSetLinkEnd);
        menu.addSeparator();

        JMenu menunet = new JMenu("网络参数配置管理");

        SNMPBaseAction action = new SNMPGetMibSystem();
        action.putValue("ActionDataKeyName", nodeDevice);
        menunet.add(action);

        SNMPBaseAction action1 = new MonitorIfTableAction();
        action.putValue("ActionDataKeyName", nodeDevice);
        menunet.add(action1);

        SNMPBaseAction action2 = new SNMPGetIfNetStatus();
        action.putValue("ActionDataKeyName", nodeDevice);
        menunet.add(action2);

        SNMPBaseAction action3 = new SNMPGetIP();
        action.putValue("ActionDataKeyName", nodeDevice);
        menunet.add(action3);

        SNMPBaseAction action4 = new SNMPGetIPAddr();
        action.putValue("ActionDataKeyName", nodeDevice);
        menunet.add(action4);

        SNMPBaseAction action5 = new SNMPGetRouterInfo();
        action.putValue("ActionDataKeyName", nodeDevice);
        menunet.add(action5);

        SNMPBaseAction action6 = new SNMPGetMAC();
        action.putValue("ActionDataKeyName", nodeDevice);
        menunet.add(action6);

        SNMPBaseAction action7 = new SNMPGetMACAndPort();
        action.putValue("ActionDataKeyName", nodeDevice);
        menunet.add(action7);

        SNMPBaseAction action8 = new SNMPGetMACIPAction();
        action.putValue("ActionDataKeyName", nodeDevice);
        menunet.add(action8);

        SNMPBaseAction action8_2 = new SNMPGetSwitchSFP();
        action.putValue("ActionDataKeyName", nodeDevice);
        menunet.add(action8_2);

        SNMPDiscoverSwitchPC action9 = new SNMPDiscoverSwitchPC();
        action.putValue("ActionDataKeyName", nodeDevice);
        menunet.add(action9);

        SNMPBaseAction action10 = new SNMPGetTCPConn();
        action.putValue("ActionDataKeyName", nodeDevice);
        menunet.add(action10);

        SNMPBaseAction action11 = new SNMPGetMibUdpEntry();
        action.putValue("ActionDataKeyName", nodeDevice);
        menunet.add(action11);

        SNMPBaseAction action12 = new SNMPGetSnmp();
        action.putValue("ActionDataKeyName", nodeDevice);
        menunet.add(action12);

        menu.add(menunet);

        JMenu menuhost = new JMenu("主机资源配置管理");

        SNMPBaseAction action13 = new SNMPGetDiskMemo();
        action.putValue("ActionDataKeyName", nodeDevice);
        menuhost.add(action13);

        SNMPBaseAction action14 = new SNMPGetMibDevice();
        action.putValue("ActionDataKeyName", nodeDevice);
        menuhost.add(action14);

        SNMPBaseAction action15 = new SNMPGetCpu();
        action.putValue("ActionDataKeyName", nodeDevice);
        menuhost.add(action15);

        SNMPBaseAction action15_2 = new SNMPGetInstallSw();
        action.putValue("ActionDataKeyName", nodeDevice);
        menuhost.add(action15_2);

        SNMPBaseAction action16 = new SNMPGetRunSW();
        action.putValue("ActionDataKeyName", nodeDevice);
        menuhost.add(action16);

        RunSWCpuAndMemAction action17 = new RunSWCpuAndMemAction();
        action.putValue("ActionDataKeyName", nodeDevice);
        menuhost.add(action17);

        menu.add(menuhost);
        menu.addSeparator();

        SNMPBaseAction action18 = new MibBrowserAction();
        action.putValue("ActionDataKeyName", nodeDevice);
        menu.add(action18);
    }

    private void setNodeLinkRightMenu(JPopupMenu menu, NodeLink nodeLink, TopoGraphView topoView) {
        LinkViewAction viewAction = new LinkViewAction();
        viewAction.putValue("ActionDataKeyName", nodeLink);

        menu.add(viewAction);
        menu.addSeparator();

        LinkDeleteAction deleteAction = new LinkDeleteAction();
        deleteAction.putValue("ActionDataKeyName", nodeLink);

        menu.add(deleteAction);
    }
}